use lib_utils::envs::get_env;
use std::sync::OnceLock;

pub fn db_config() -> &'static DbConfig {
    static INSTANCE: OnceLock<DbConfig> = OnceLock::new();

    INSTANCE.get_or_init(|| {
        DbConfig::load_from_env()
            .unwrap_or_else(|ex| panic!("FATAL - WHILE LOADING CONF - Cause: {ex:?}"))
    })
}

#[allow(non_snake_case)]
pub struct DbConfig {
    pub PG_POSTGRES_URL: String,
    pub PG_APP_URL: String,
}

impl DbConfig {
    fn load_from_env() -> lib_utils::envs::Result<DbConfig> {
        Ok(DbConfig {
            PG_POSTGRES_URL: get_env("PG_POSTGRES_URL")?,
            PG_APP_URL: get_env("PG_APP_URL")?,
        })
    }
}
